/*
 * @Description: wcy
 * @Author: cy2020
 * @Date: 2022-02-25 09:05:33
 * @LastEditTime: 2022-02-25 19:44:54
 */
const iot = require('../../src/iot');
const { log, getAuthInfo, getKits, getCOQ } = require('../../src/utils');
const data = require('../data/pcrData.json');
const deviceClient = require("./devices");
const { v4: uuidv4 } = require('uuid');

// const connectUrl = 'mqtt://192.168.9.36:1883';
const connectUrl = 'mqtt://192.168.50.93:1883';

log.info('------------------------ 认证鉴权 ------------------------');
const deviceIot = iot.device(
    {
        connectUrl, // mqtt服务器地址
        deviceName: deviceClient.deviceId, // 设备信息
        productKeys: deviceClient.deviceId, //
        deviceSecret: deviceClient.configuration.secureKey,
    },
    /**
     *                                             
     * 
     * 认证： 获取用户名和密码
     * 支持自定义认证规则
     * @param {} deviceName
     * @param {*} productKeys
     * @param {*} deviceSecret
     * @returns{ username,passward}
     */
    function (deviceName, productKeys, deviceSecret) {
        return getAuthInfo(deviceName, deviceSecret);
    }
);



// iot.device(
//     {
//         connectUrl, // mqtt服务器地址
//         deviceName: getCOQ(), // 设备信息
//         productKeys: deviceClient.deviceId, //
//         deviceSecret: deviceClient.configuration.secureKey,
//     },
//     /**
//      *                                             
//      * 
//      * 认证： 获取用户名和密码0
//      * 支持自定义认证规则
//      * @param {} deviceName
//      * @param {*} productKeys
//      * @param {*} deviceSecret
//      * @returns{ username,passward}
//      */
//     function (deviceName, productKeys, deviceSecret) {
//         return getAuthInfo(deviceName, deviceSecret);
//     }
// );


log.info('------------------------ 测试传送实验数据 ------------------------');
//测试传送实验数据
setInterval(publish, 5000);

/**
 * 推送实验数据
 * topic格式为
 */

function publish() {
    let myData = {...data}
    myData.deviceNo =  deviceClient.deviceId;
    myData.createTime = new Date().getTime();
    myData.startTime = new Date().getTime() + 5 * 3600;
    myData.serialNo = deviceClient.deviceId.replace("-","") + myData.create_time;
    myData.uuid = uuidv4();
    myData.kitNo = getKits();;

    // let eventsPcrDataMessage = {
    //     deviceId:  deviceClient.deviceId,
    //     messageId: uuidv4(),
    //     timestamp: new Date().getTime(),
    //     data: JSON.stringify(myData),
    // };
    // // 事件信息
    // deviceIot.publish(
    //     `/device/CarryOnP1000Q/${deviceClient.deviceId}/events/pcrData`,
    //     JSON.stringify(eventsPcrDataMessage),
    //     {
    //         qos: 0,
    //     },
    //     function (error) {
    //         log.info(error);
    //     }
    // );

    // let propertiesMessage = {
    //     deviceId:  deviceClient.deviceId,
    //     messageId: uuidv4(),
    //     timestamp: new Date().getTime(),
    //     data: JSON.stringify({"value":20}),
    // }    

    // // 属性信息
    // deviceIot.publish(
    //     `/device/CarryOnP1000Q/${deviceClient.deviceId}/properties/pcrName`,
    //     JSON.stringify(propertiesMessage),
    //     {
    //         qos: 0,
    //     },
    //     function (error) {
    //         log.info(error);
    //     }
    // );    
    let tagPcrDataMessage = {
        deviceId:  deviceClient.deviceId,
        messageId: uuidv4(),
        timestamp: new Date().getTime(),
        data: JSON.stringify(myData),
    };
    // 事件信息
    deviceIot.publish(
        `/device/P1000Q/${deviceClient.deviceId}/tag/checkdata`,
        JSON.stringify(tagPcrDataMessage),
        {
            qos: 0,
        },
        function (error) {
            log.info(error);
        }
    );    
}

// log.info('------------------------ 测试接收服务器反馈------------------------');
deviceIot.subscribe(
        `/device/P1000Q/${deviceClient.deviceId}/pong/checkdata`,
        { qos: 2 },
        function (error, granted) {
            if (error) {
                console.log(error);
            } else {
                console.log(`${granted[0].topic} was subscribed`);
            }
        }
);


